<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">

    <resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
        <#list table.fields as field>
            <#if field.keyFlag>
                <id column="${field.name}" property="${field.propertyName}" />
            <#else>
                <result column="${field.name}" property="${field.propertyName}" />
            </#if>
        </#list>
    </resultMap>

    <sql id="Base_Column_List">
        <#list table.commonFields as field>${field.name},</#list> ${table.fieldNames}
    </sql>

    <select id="selectById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM ${table.name}
        WHERE ${table.name.id} = #{id}
    </select>

    <select id="selectList" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM ${table.name}
        <where>
            <#list table.fields as field>
                <if test="${field.propertyName} != null">
                    AND ${field.name} = #{${field.propertyName}}
                </if>
            </#list>
        </where>
    </select>

    <insert id="insert" parameterType="${package.Entity}.${entity}" <#if table.keyProperty??>useGeneratedKeys="true" keyProperty="${table.keyProperty}"</#if>>
        INSERT INTO ${table.name}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#list table.fields as field>
                <if test="${field.propertyName} != null">
                    ${field.name},
                </if>
            </#list>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <#list table.fields as field>
                <if test="${field.propertyName} != null">
                    #{${field.propertyName}},
                </if>
            </#list>
        </trim>
    </insert>

    <update id="updateById" parameterType="${package.Entity}.${entity}">
        UPDATE ${table.name}
        <set>
            <#list table.commonFields as field>
                <if test="${field.propertyName} != null">
                    ${field.name} = #{${field.propertyName}},
                </if>
            </#list>
        </set>
        WHERE ${table.keyColumn} = "#{"+${table.keyProperty}+"}"
    </update>

    <delete id="deleteById">
        DELETE FROM ${table.name}
        WHERE ${table.keyColumn} =#{${table.keyProperty}}
    </delete>

</mapper>
